<!DOCTYPE html>
<html>
<head>
<title>分配权限</title>
#include("/admin/_common/head.html")
<!-- 样 式 文 件 -->
</head>
<!-- 代 码 结 构 -->
<body class="pear-container">
    <form class="layui-form layui-form-pane">
        <input type="hidden" name="id" value="#(bean.id??)">
        <input type="hidden" name="permissionIds">
        
        <div class="layui-card">
            <div class="layui-card-header">基本信息</div>
            <div class="layui-card-body">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-inline">
                        <input autocomplete="off" class="layui-input" disabled value="#(bean.name)" />
                    </div>
                    <label class="layui-form-label">权限字符串</label>
                    <div class="layui-input-inline">
                        <input autocomplete="off" class="layui-input" disabled value="#(bean.code)" />
                    </div>
                </div>
            </div>
            <div class="layui-card-header">分配系统权限</div>
            <div class="layui-card-body">
	            <div class="layui-panel">
	            	<div id="beans-tree"></div>
	            </div>
            </div>
        </div>
        <div class="bottom" style="display: none;">
            <div class="button-container">
                <button onclick="window.checkData();" type="submit" class="pear-btn pear-btn-primary pear-btn-xs" lay-submit="" lay-filter="data-save" id="data-save" onclick="window.checkRoles();">
                    <i class="layui-icon layui-icon-ok"></i> 提交
                </button>
            </div>
        </div>
    </form>
    
    <!-- 图标字段 -->
    <script type="text/html" id="icon">
        <i class="layui-icon {{d.icon}}"></i>
    </script>
    
    <!-- 类型字段 -->
    <script type="text/html" id="typeName">
    <!-- #DICTS("permission_type")#for(item : dicts) -->
    #[[{{# if(d.type == ']]##(item.value)#[[') { }}]]#
        <span class="#(item.listClass)">#(item.label)</span>
    #[[{{# } }}]]#
    <!-- #end#end -->
    </script>
<!-- 资 源 引 入 -->
#include("/admin/_common/foot.html")
#include("/admin/_common/form_style.html")
<script>
layui.use(['form', 'jquery', 'common'], function() {
    var $ = layui.jquery;
    var form = layui.form;
    var common = layui.common;
    var tree = layui.tree;
    
    /*
     * 刷新表格
     */
    window.renderTree = () => {
    	var data = window.getData();
    	
        tree.render({
            elem: '#beans-tree',
            id: 'beans-tree-1',
            data: data,
            method: "get",
            showCheckbox: true,  // 是否显示复选框
            height: 'full-95',
            accordion: true
        });

        treeFn.selectChecked('beans-tree-1', data, window.getCheckedIds());
    };
    
    window.getData = () => {
        var data = [];
        $.ajax({
            url: "#CTX()/admin/system/permission/tree?spread=3",    //后台数据请求地址
            type: "get",
            async: false,
            success: function(result){
                data = result;
            }
        });
        return data;
    };
    
    window.getCheckedIds = () => {
    	var data = [];
        $.ajax({
            url: "#CTX()/admin/system/permission/queryByRole?roleId=#(bean.id??)",    //后台数据请求地址
            type: "get",
            async: false,
            success: function(result){
                for (var i = 0; i < result.length; i++){
                	data.push(result[i].id)
                }
            }
        });
        return data;
    };

    window.renderTree();
    
    window.checkData = () => {
        $("[name='permissionIds']").val(treeFn.getCheckedIds("beans-tree-1").join());
    };
});
</script>
</body>
</html>
